<?php defined("MAIN_DIR") or die("No direct access");

$limit = 30;
$page = UV::rInt(strip_tags($route->GET("page")));
if ($page < 1){
	$page = 1;
}

$query = "select SQL_CALC_FOUND_ROWS `P`.`id`,`P`.`file`,`P`.`text`,`P`.`switch`
	from `photos` as `P`
	order by `P`.`id`
    limit 100";
$list = $db->getTable($query);

$count = count($list);
$all_count = $db->getCell("SELECT FOUND_ROWS()");

require_once ADMIN.HEADFILE;
?>
<script type="text/javascript" src="/files/js/fancybox/jquery.fancybox.js"></script>
<link rel="stylesheet" type="text/css" href="/files/js/fancybox/jquery.fancybox.css" media="screen" />

<script type="text/javascript" src="/files/js/swfupload/swfupload.js"></script>

<h1 class='with_button'>
	<div style="float:left">Фотки</div>
	<div class='button load-photo-button'><span id='load-images'></span></div>
	<img id='loading' src="/files/img/admin/loading.gif" />
</h1>
<textarea id='rename_photo' ></textarea>

<script type="text/javascript">

	var content = null;
	hiddenDiv = $(document.createElement('div')),
	hiddenDiv.addClass('hiddendiv');
	$('body').append(hiddenDiv);

	var fancyoptions = {
		padding: 2,
		openEffect : 'elastic',
		openSpeed  : 150,
		closeEffect	: 'none'
	};

	function createSelection(field, start, end) {
		if( field.createTextRange ) {
			var selRange = field.createTextRange();
			selRange.collapse(true);
			selRange.moveStart('character', start);
			selRange.moveEnd('character', end);
			selRange.select();
		} else if( field.setSelectionRange ) {
			field.setSelectionRange(start, end);
		} else if( field.selectionStart ) {
			field.selectionStart = start;
			field.selectionEnd = end;
		}
		field.focus();
	}

	var edit_id = 0;

	var swfu;
	// handlers
	var UploadSuccessEventHandler = function (file, server_data, receivedResponse) {
		alert(server_data);
	};
	function selectFilesCompleteEventHandler(numFilesSelected, numFilesQueued) {
		try {
			if (numFilesQueued > 0) {
				this.startUpload();
				$("#loading").show(0);
			}
		} catch (ex) {
			this.debug(ex);
		}
	}
	function uploadSuccess(file, serverData) {

		var request = $.parseJSON(serverData);

		var photo_item = document.createElement("div");
			photo_item.className = "photo_item";
			photo_item.id = "photo_item_"+request.id;
		var photo_container = document.createElement("div");
			photo_container.className = "photo_container";
		var photo_info = document.createElement("div");
			photo_info.className = "photo_info";
		var photo_text = document.createElement("div");
			photo_text.className = "photo_text";
			photo_text.id = "photo_text_"+request.id;
			photo_text.innerHTML = "Без текста";
			photo_text.title = "Изменить текст";
		var photo_links = document.createElement("a");
			photo_links.className = "photo_links";
			photo_links.id = "photo_"+request.id;
			photo_links.href = request.big;
			photo_links.title = "Без названия";
			photo_links.rel = "group";
		var photo = document.createElement("img");
			photo.className = "photo";
			photo.src = request.small;
		$(photo_links).append(photo);
		$(photo_container).append(photo_text,photo_links);
		var photo_info = document.createElement("div");
			photo_info.className = "photo_info";
		var action_on = document.createElement("a");
			action_on.className = "action-on";
			action_on.href = "/admin/photos/on/"+request.id;
		var img_on = document.createElement("img");
			img_on.alt = img_on.title = "Включить";
			img_on.src = "/files/img/admin/on.png";
		var action_trash = document.createElement("a");
			action_trash.className = "action-trash";
			action_trash.href = "/admin/photos/trash/"+request.id;
		var img_trash = document.createElement("img");
			img_trash.alt = img_trash.title = "Удалить";
			img_trash.src = "/files/img/admin/trash.png";
		$(action_on).append(img_on);
		$(action_trash).append(img_trash);
		$(photo_info).append(action_on,action_trash);

		$(photo_item).append(photo_container,photo_info).css("display","none");
		$("#photo_list").append(photo_item);
		$(photo_item).fadeIn(300);
	}
	function uploadComplete(file) {
		try {
			if (this.getStats().files_queued > 0) {
				this.startUpload();
			} else {
				$("#loading").hide(0);
				$('.photo_links').fancybox(fancyoptions);
			}
		} catch (ex) {
			this.debug(ex);
		}
	}

	window.onload = function(){

		var settings_object = {

			// Backend Settings
			upload_url : "/admin/photos/upload",

			// File Upload Settings
			flash_url : "/files/js/swfupload/swfupload.swf",
			file_size_limit : "10 MB",
			file_post_name : "Filedata",
			file_types : "*.jpg;*.jpeg;*.png",
			file_types_description : "Image files",
			file_upload_limit : 0,

			// GUI
			button_placeholder_id : "load-images",
			button_width: 150,
			button_height: 28,
			button_text : '<span class="load-button">Добавить фотки</span>',
			button_text_style : '.load-button { font-size: 13pt; font-family: Segoe UI; color: #DCE2EA; }',
			button_text_top_padding: 2,
			button_text_left_padding: 34,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,

			// handlers
			upload_success_handler : UploadSuccessEventHandler,
			file_dialog_complete_handler : selectFilesCompleteEventHandler,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete
		};

		swfu = new SWFUpload(settings_object);
	};

	$(document).ready(function(){

		// fancybox
		$('.photo_links').fancybox(fancyoptions);

		// search
		$("#photo_search").keyup(function(){
			var query = $(this).val().toLowerCase();
			$(".photo_item").each(function(){
				if ($(this).find(".photo_text").text().toLowerCase().search(query) == -1){
					$(this).hide();
				} else {
					$(this).show();
				}
			})
		});

		// show rename
		$(".photo_text").live("click",function(){
			var text = $.trim($(this).text());
			var height = $(this).height();
			$("#rename_photo")
				.css({
					"top":$(this).offset().top-1,
					"left":$(this).offset().left-1
				})
				.val(text)
				.fadeIn(100)
				.height(height)
				.focus();
			var el = document.getElementById("rename_photo");
			createSelection(el,el.value.length,el.value.length);
			edit_id = $(this).attr("id").substr(11);
		});

		// rename or hide // autoheight
		$("#rename_photo").blur(function(){
			$(this).hide(0);
		}).keydown(function(e){

			if (e.keyCode == 13){
				var text = $(this).val();
				if ($.trim(text) == ""){
					$(this).hide(0);
					return false;
				}
				$.ajax({
					url: "/admin/photos/rename",
					data: "id="+edit_id+"&text="+text,
					type: "post",
					success: function(data){
						if (data = "success"){
							$("#photo_text_"+edit_id).text(text);
							$("#photo_"+edit_id).attr("title",text);
						}
					}
				});
				$(this).hide(0);
				return false;
			} else if (e.keyCode == 32) {
				if ($(this).val()[$(this).val().length - 1] == " ")
					return false;
			} else if (e.keyCode == 27) {
				$(this).hide(0);
				return false;
			}
		}).keyup(function(e){

			// auto height
			content = $(this).val().replace(/\n/g, '<br>');
			hiddenDiv.html(content);
			$(this).css('height', hiddenDiv.height());
			var top = $("#photo_text_"+edit_id).offset().top + $("#photo_text_"+edit_id).height() - $(this).height() - 1;
			$(this).css({"top":top});
		})

		// on/off
		$(".action-on").live("click",function(){
			var link = $(this);
			$.ajax({
				url: link.attr("href"),
				success: function(data){
					if (data){
						link
							.removeClass("action-on")
							.addClass("action-off")
							.attr("href","/admin/photos/off/"+data)
							.find("img")
								.attr({"src":"/files/img/admin/on.png","title":"Выключить","alt":"Выключить"});
					}
				}
			})
			return false;
		});
		$(".action-off").live("click",function(){
			var link = $(this);
			$.ajax({
				url: link.attr("href"),
				success: function(data){
					if (data){
						link
							.removeClass("action-off")
							.addClass("action-on")
							.attr("href","/admin/photos/on/"+data)
							.find("img")
								.attr({"src":"/files/img/admin/off.png","title":"Включить","alt":"Включить"});
					}
				}
			})
			return false;
		});
		$(".action-trash").live("click",function(){
			var link = $(this);
			$.ajax({
				url: link.attr("href"),
				success: function(data){
					if (data){
						$("#photo_item_"+data).remove();
					}
				}
			})
			return false;
		});
	})
</script>

<div class='photo_search'>
	<input type='text' placeholder='Поиск' id='photo_search' />
</div>
<div id="photo_list" class='photo_list'><?php
foreach ($list as $p) {
	if ($p['text'] == ""){
		$p['text'] = "Без названия";
	}
	?>
	<div class='photo_item' id="photo_item_<?php echo $p['id']; ?>">
		<div class='photo_container'>
			<div class='photo_text' id='photo_text_<?php echo $p['id'];?>' title="Изменить текст">
				<?php echo $p['text']; ?>
			</div>
			<a rel="group" id="photo_<?php echo $p['id']; ?>" class='photo_links' href="/files/img/photos/x_<?php echo $p['file']; ?>" title="<?php echo $p['text']; ?>">
				<img class='photo' src='/files/img/photos/z_<?php echo $p['file']; ?>' />
			</a>
		</div>
		<div class='photo_info'><?php
			if ($p['switch']){
				echo "<a class='action-off' href='/admin/photos/off/{$p['id']}'><img src='/files/img/admin/on.png' alt='Выключить' title='Выключить'></a>";
			} else {
				echo "<a class='action-on' href='/admin/photos/on/{$p['id']}'><img src='/files/img/admin/off.png' alt='Включить' title='Включить'></a>";
			}
			?>
			<a class='action-trash' href='/admin/photos/trash/<?php echo $p['id']; ?>'><img src='/files/img/admin/trash.png' alt='Удалить' title='Удалить'></a>
		</div>
	</div><?php
}
?><div><?php

echo showPages($all_count, $limit);

require_once ADMIN.FOOTERFILE; ?>